﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EislaufScb.WebForms.ViewModel.Person;
using System.IO;
using System.Web.Services;

namespace EislaufScb.WebForms.Views.Personal
{
    public partial class Skater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SkaterDataList.DataSource = SkaterViewModel.GetSkaters();
            SkaterDataList.DataBind();
        }

        protected void SkaterDataList_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            Image personImage = (Image)e.Item.Controls.OfType<Image>().FirstOrDefault();
            BulletedList certificationList = (BulletedList)e.Item.Controls.OfType<BulletedList>().FirstOrDefault();

            SkaterViewModel person = (SkaterViewModel)e.Item.DataItem;

            FileInfo file = new DirectoryInfo(Server.MapPath("~/Images/Skaters")).GetFiles().Where(f => f.Name.Contains(person.LastName) || f.Name.Contains(person.LastName)).FirstOrDefault();

            //person.Certifications.ToList().ForEach(cert => certificationList.Items.Add(new ListItem() { Text = cert }));

            string imageUrl = string.Empty;
            if (file != null)
            {
                imageUrl = string.Format(@"~/Images/Skaters/{0}", file.Name);
            }

            personImage.ImageUrl = imageUrl;

            personImage.Attributes.Add("onclick", string.Format(@"ShowSkaterDetails('{0}','{1}','{2}')", person.FirstName, person.LastName, personImage.ClientID));
        }

        [WebMethod]
        public static SkaterViewModel GetSkaterDetails(string firstName, string lastName)
        {
            return SkaterViewModel.GetSkaterDetails(firstName, lastName);
        }
    }
}